package com.yDev.rockclimber;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.ColorDrawable;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity{
	
	StickMan sm;
	
	private Menu menu;
	
	Boolean gameState=true;
	
	
	
	 private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // set our MainGamePanel as the View
       
        ScrollView sv=new ScrollView(getApplicationContext());
//       / sv.addView(new StickMan(this));
        sm=new StickMan(this);
        setContentView(sm);
        Log.d(TAG, "View added");
       // sm=new StickMan(getApplicationContext());
        
      //  setContentView(sm);
        
        

    }
    
/*    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        
    	if(!gameState){
    		viewAddRock.show();
    	}
    	
        return super.onTouchEvent(event);//false indicates the event is not consumed
    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        this.menu = menu;
        return true;
    }
 
    public boolean saveClicked(MenuItem item) {
    	String mScreenshotPath = Environment.getExternalStorageDirectory()+"";
    	   Bitmap bitmap = Bitmap.createBitmap(sm.getWidth(), sm.getHeight(), Bitmap.Config.ARGB_8888);
    	   sm.onDraw(new Canvas(bitmap));
    	   try {
    	       OutputStream out = new BufferedOutputStream(new FileOutputStream(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg"));
    	       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    	   } catch (IOException e) {
    	       Log.w(TAG, e);
    	   }
        return true;
    }
    
    public boolean gameClicked(MenuItem item){
    	
    	item.setChecked(true);
    	
    	menu.findItem(R.id.menu_add_rock).setChecked(false);
    	sm.setGameState(true);
    	gameState=true;
    	
    	 return true;
    }
    
public boolean addRockClicked(MenuItem item){
    	
    	item.setChecked(true);
    	
    	menu.findItem(R.id.menu_game).setChecked(false);
    	sm.setGameState(false);
    	gameState=false;
    	
    	 return true;
    }

public boolean saveMapClicked(MenuItem item){
	String mScreenshotPath = Environment.getExternalStorageDirectory()+"";
	sm.saveMap(mScreenshotPath + "/" + System.currentTimeMillis() + ".txt");
	
	 return true;
}

    
}
